<template>
  <div class="app">
    <!-- 收藏头部 -->
    <van-nav-bar
      title="收藏列表"
      left-arrow
      @click-left="onLeft"
    />
    <!-- /收藏头部 -->
    <van-pull-refresh
    v-model="isLoading"
    @refresh="onRefresh"
    :success-text='tipText'
    success-duration="1500">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
      <!-- :to="'/detail/'+item.houseCode" -->
        <van-cell v-for="(item,index) in list" :key="index" #default @click="toFangWu(item)">
          <template>
            <div class="cell-img">
              <img :src="'http://liufusong.top:8080'+item.houseImg" alt="">
            </div>
            <div class="cell-content">
              <h3 class="van-ellipsis">{{item.title}}</h3>
              <p class="cell-desc">{{item.desc}}</p>
              <van-tag
              color="#e1f5f8"
              text-color="#39becd"
              v-for="item in item.tags"
              :key="item">{{item}}</van-tag>
              <div class="cell-price">
                <span>{{item.price}}</span>元/月
              </div>
            </div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getFavorate } from '@/api/login'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      error: false,
      isLoading: false,
      tipText: ''
    }
  },

  created () {

  },

  methods: {
    onLeft () {
      this.$router.back()
    },
    async onLoad () {
      // 发送请求获取数据
      try {
        const res = await getFavorate()
        console.log(res)
        // console.log(...res)
        // 保存数据
        // this.list.push(...res)
        this.list = res
        // 当前这一次的加载状态结束
        this.loading = false
        // 数据全部加载完成
        this.finished = true
        // if (!res.length) {
        //   this.finished = true
        // }
      } catch (err) {
        console.log('获取失败')
        this.error = true
        this.loading = false
      }
    },
    async onRefresh () {
      try {
        const res = await getFavorate()
        console.log(res)
        console.log(...res)
        // this.list.push(...res)
        this.list = res
        this.isLoading = false
        this.tipText = '刷新成功'
      } catch (err) {
        console.log('获取失败')
        this.isLoading = false
        this.tipText = '刷新失败，再次尝试'
      }
    },
    toFangWu (item) {
      console.log(item.houseCode)
      this.$router.push({
        path: '/detail/' + item.houseCode,
        params: {
          id: item.houseCode
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>
.app {
  .van-nav-bar {
    background-color: #21b97a;
    /deep/.van-nav-bar__title {
      font-size: 18px;
      color: #fff;
    }
    /deep/.van-icon{
      color: #fff;
    }
  }
  .van-cell {
    height: 120px;
    position: relative;
    box-sizing: border-box;
    justify-content: space-around;
    padding-top: 18px;
    border-bottom: 1px solid #e5e5e5;
    .cell-img {
      float: left;
      width: 106px;
      height: 80px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .cell-content {
      overflow: hidden;
      line-height: 22px;
      padding-left: 12px;
      h3 {
        margin: 0;
      }
      .cell-desc {
        margin: 0;
        font-size: 12px;
        color: #afb2b3;
      }
      .cell-price {
        margin: 0;
        font-size: 12px;
        color: #fa5741;
        span {
          font-weight: 700;
          font-size: 16px;
        }
      }
    }
  }
}
</style>
